package com.tul.exercise.base.swing.components;

import com.tul.exercise.base.swing.SwingConsole;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.List;

/**
 * @Description 《java编程思想》第22章 示例17：
 * <p>
 * 消息框
 **/
public class MessageBoxes extends JFrame {
    private List<JButton> jbList = Arrays.asList(
            new JButton("Alert"), new JButton("Yes/No"),
            new JButton("Color"), new JButton("Input"), new JButton("3 Vals")
    );

    private JTextField textField = new JTextField(15);
    private ActionListener al = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            String id = ((JButton) e.getSource()).getText();

            switch (id) {
                case "Alert":
                    JOptionPane.showMessageDialog(null, "There's a bug on you!",
                            "Hey", JOptionPane.ERROR_MESSAGE);
                    break;
                case "Yes/No":
                    JOptionPane.showConfirmDialog(null, "or no",
                            "choos yes", JOptionPane.YES_NO_OPTION);
                    break;
                case "Color":
                    List<Object> options = Arrays.asList("Red", "Green");
                    int sel = JOptionPane.showOptionDialog(null, "Choose a Color!",
                            "Warning", JOptionPane.DEFAULT_OPTION,
                            JOptionPane.WARNING_MESSAGE, null, options.toArray(), options.get(0));
                    break;
                case "Input":
                    String val = JOptionPane.showInputDialog("How many fingers do you see?");
                    textField.setToolTipText(val);
                    break;
                case "3 Vals":
                    Object[] selections = {"First", "Second", "Third"};
                    Object value = JOptionPane.showInputDialog(null, "Choos one", "Input",
                            JOptionPane.INFORMATION_MESSAGE, null, selections, selections[0]);
                    if (value != null) {
                        textField.setText(value.toString());
                    }
                    break;
                default:

            }

            if (id.equals("Alert")) {
                JOptionPane.showMessageDialog(null, "There's a bug on you!", "Hey", JOptionPane.ERROR_MESSAGE);
            }

        }
    };

    public MessageBoxes() {
        setLayout(new FlowLayout());
        for (int i = 0; i < jbList.size(); i++) {
            jbList.get(i).addActionListener(al);
            add(jbList.get(i));
        }

        add(textField);
        //textField.addActionListener(al);

    }

    public static void main(String[] args) {
        SwingConsole.run(new MessageBoxes(), 200, 200);
    }
}
